package com.ow.dva.module.passingRecord.service.impl;

import com.alibaba.fastjson.JSON;
import com.ow.dva.module.base.entity.param.DvaPage;
import com.ow.dva.module.base.service.impl.DvaServiceImpl;
import com.ow.dva.module.front.entity.Fold;
import com.ow.dva.module.park.entity.Park;
import com.ow.dva.module.park.service.ParkService;
import com.ow.dva.module.passingRecord.entity.PassingRecord;
import com.ow.dva.module.passingRecord.mapper.PassingRecordMapper;
import com.ow.dva.module.passingRecord.service.PassingRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 过车记录表 服务实现类
 * </p>
 *
 * @author YanZengBao
 * @since 2020-02-19
 * @version V1.0
 */
@Service
public class PassingRecordServiceImpl extends DvaServiceImpl<PassingRecordMapper, PassingRecord> implements PassingRecordService {

    @Resource
    private ParkService parkService;
//    @DvaCache5M
    @Override
    public DvaPage<PassingRecord> page(DvaPage<PassingRecord> page, Map<String, Object> param) {

        return baseMapper.page(page,param);
    }

    @Override
    public Integer passRecUpLoad(PassingRecord passingRecord) {
        return baseMapper.insert(passingRecord);
    }

    @Override
    public List<PassingRecord> getTodayPassingRecords(String roadId) {
        return baseMapper.getTodayPassingRecords(roadId);
    }
        @Override
    public List<PassingRecord> getTodayByPark(String parkId) {
        return baseMapper.getTodayByPark(parkId);
    }
        @Override
    public List<PassingRecord> weekDataByPark(String parkId) {
        return baseMapper.weekDataByPark(parkId);
    }

 @Override
    public List<PassingRecord> getAllTodayPassingRecords() {
        return baseMapper.getAllTodayPassingRecords();
    }

    @Override
    public int getJsonRecord(String beanStr) {
        PassingRecord passingRecord = JSON.parseObject(beanStr,PassingRecord.class);
        return baseMapper.insert(passingRecord);
    }

    /**
     * 模糊匹配过车记录
     * @param plate
     * @return
     */
    @Override
    public List<PassingRecord> matchPassRecord(String plate,String parkId) {
        return baseMapper.matchPassRecord(plate,parkId);
    }

    @Override
    public List<PassingRecord> getWeekRecordByPlate(String parkId, String plateNo) {
        return baseMapper.getWeekRecordByPlate(parkId,plateNo);
    }

    @Override
    public List<PassingRecord> getPassRecordByPlateMarker(String parkId, String plateNo) {
        List<PassingRecord> passingRecords = baseMapper.getWeekRecordByPlate(parkId,plateNo);
        for (int i = 0; i< passingRecords.size(); i++){
            for (int j=passingRecords.size()-1; j>i ; j--){
                if (passingRecords.get(j).getParkId().equals(passingRecords.get(i).getParkId())){
                    passingRecords.remove(j);
                }
            }
        }
        return addPassingRecordPoint(passingRecords);
    }

    @Override
    public List<Fold> countPassRecordByPark(String parkId) {
        List<Fold> foldList = new ArrayList<>();
        Date now = new Date();
        long nowTime = now.getTime();
        for (int i=6 ; i>=0 ;i--){
            SimpleDateFormat outFormat = new SimpleDateFormat("MM-dd");
            long time = nowTime - 86400000 * i;
            Date rDate = new Date(time);
            long count = baseMapper.countDayPassData(parkId,rDate);
            Fold fold = new Fold();
            fold.setValue(String.valueOf(count));
            String axi = outFormat.format(rDate);
            fold.setAxis(axi);
            foldList.add(fold);
        }

        return foldList;
    }


    @Override
    public PassingRecord getNewPassRecordByPark(String parkId, int direction) {
         PassingRecord passingRecord = baseMapper.getNewPassRecordByPark(parkId,direction);
         return passingRecord;
    }


    @Override
    public List<PassingRecord> getNewPassingRecord(List list) {
        return baseMapper.getNewPassingRecord(list);
    }

    /**
     * 给过车记录列表添加停车场经纬度
     * @param passingRecords
     * @return
     */
    public List<PassingRecord> addPassingRecordPoint(List<PassingRecord> passingRecords){
        List<PassingRecord> passingRecordList = new ArrayList<>();
        for (PassingRecord passingRecord : passingRecords){
            Park park = parkService.getById(passingRecord.getParkId());
            passingRecord.setLat(park.getLatitude());
            passingRecord.setLng(park.getLongitude());
            passingRecordList.add(passingRecord);
        }
        return passingRecordList;

    }
}
